﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Dialogs;
using System.IO;
using System.Xml.Serialization;

namespace ReactXML
{
    /// <summary>
    /// FindFile.xaml 的交互逻辑
    /// </summary>
    public partial class FindFile : Window
    {
        private string m_szLastDirPath = ".";

        private string m_szSaveFile = "./result.txt";

        private void TextBox_PreviewDragOver(object sender, System.Windows.DragEventArgs e)
        {
            e.Effects = System.Windows.DragDropEffects.Copy;
            e.Handled = true;
        }
    
        private void TextBox_PreviewDrop(object sender, System.Windows.DragEventArgs e)
        {
            object text = e.Data.GetData(System.Windows.DataFormats.FileDrop);
            System.Windows.Controls.TextBox tb = sender as System.Windows.Controls.TextBox;
            if (tb != null)
            {
                tb.Text = string.Format("{0}", ((string[])text)[0]);
            }
        }

        private void Wnd_PreviewDragOver (object sender, System.Windows.DragEventArgs e)
        {
            object text = e.Data.GetData(System.Windows.DataFormats.FileDrop);
            if (text != null)
            {
                e.Effects = System.Windows.DragDropEffects.Copy;
                e.Handled = true;
            }
        }

        private void Wnd_PreviewDrop (object sender, System.Windows.DragEventArgs e)
        {
            object text = e.Data.GetData(System.Windows.DataFormats.FileDrop);
            if (text != null) 
            {
                string szPath = ((string[])text)[0];

                if (szPath.Contains(".") == true)
                    this.textBoxFilePath.Text = szPath;
                else
                    this.textBoxSearchPath.Text = szPath;
            }
        }

        public FindFile()
        {
            GameMain.gFramework.Init();
            Log.Close = false;
            GameMain.gFramework.Register<FileDatabaseSys>(initCallback);

            if (FileHelper.IsExistFile(m_szSaveFile))
            {
                FileDatabaseSys sys = GameMain.GetModule<FileDatabaseSys>();
                sys.GetComponent<DataSaveLoadComponent>().LoadFile(m_szSaveFile);
            }

            InitializeComponent();
        }

        public void initCallback (IModule m)
        {
            return;
        }

        private void openFile(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";

            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = fileDialog.FileName;
                this.textBoxFilePath.Text = path;
            }
        }

        private void openSearchPath (object sender, RoutedEventArgs e)
        {
            var dlg = new CommonOpenFileDialog();
            dlg.Title = "My Title";
            dlg.IsFolderPicker = true;
            dlg.InitialDirectory = m_szLastDirPath;

            dlg.AddToMostRecentlyUsedList = false;
            dlg.AllowNonFileSystemItems = false;
            dlg.DefaultDirectory = m_szLastDirPath;
            dlg.EnsureFileExists = true;
            dlg.EnsurePathExists = true;
            dlg.EnsureReadOnly = false;
            dlg.EnsureValidNames = true;
            dlg.Multiselect = false;
            dlg.ShowPlacesList = true;

            if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
            {
                // Do something with selected folder string
                this.textBoxSearchPath.Text = dlg.FileName;
                m_szLastDirPath = dlg.FileName;
            }
        }

        private void showInfo(string szInfo)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                //这边开始就是委托的内容
                this.labelInfo.Content = szInfo;
            }));
        }

        private void onSearch (object sender, RoutedEventArgs e)
        {
            if (FileHelper.IsFileAccessProtected(this.textBoxSearchPath.Text)
                || FileHelper.IsFileAccessProtected(this.textBoxFilePath.Text))
            {
                System.Windows.Forms.MessageBox.Show("当前访问权限不够");
            }

            FileDatabaseSys sys = GameMain.GetModule<FileDatabaseSys>();
            sys.ReadThreadCount = 1;
            string szFindFilePath = this.textBoxFilePath.Text;
            DateTime startTime = DateTime.Now;

            GameMain.GetModule<FileDatabaseSys>().LoadFolder(this.textBoxSearchPath.Text, ()=> {
            // GameMain.GetModule<FileDatabaseSys>().LoadFolder(this.textBoxSearchPath.Text, ()=> {
                string szResult = string.Empty;
                FileInfo info = sys.FindFile(szFindFilePath);
                if (info != null)
                {
                    List<string> pathDic = info.PathList;
                    string szOut = string.Empty;
                    foreach (string dic in pathDic)
                    {
                        szOut += dic + "  ";
                    }

                    szResult = "|Files found: " + "数量:[" + pathDic.Count + "] " + szOut;
                }
                else
                {
                    szResult = "No Found!";
                }

                sys.GetComponent<DataSaveLoadComponent>().SaveFile(m_szSaveFile);

                TimeSpan allTime = DateTime.Now - startTime;

                szResult = "AllFile:" + GameMain.GetModule<FileDatabaseSys>().FilePathTable.Count + " | [" + allTime.TotalMilliseconds + "]" + szResult;
                System.Windows.Forms.MessageBox.Show(szResult);
            },

            ()=> {
                string szResult = string.Format("{0}/{1}", sys.FileInsertCount, sys.FileCount);
                this.showInfo(szResult);
            });
        }
    }
}
